import { configureStore } from "@reduxjs/toolkit"
import undoable, { excludeAction, StateWithHistory } from "redux-undo"
import userReducer, { UserStateType } from "./userReducer"
import componentsReducer, { ComponentsStateType } from "./componentsReducer"
import pageInfoReducer, { PageInfoType } from "./pageInfoReducer"

export type StateType = {
    user: UserStateType

    // 没有添加undo的写法
    // components: ComponentsStateType

    // 添加了undo的写法
    components: StateWithHistory<ComponentsStateType>

    pageInfo: PageInfoType
}

const store = configureStore({
    reducer: {
        // 分模块
        user: userReducer,

        //#region 没有undo的写法
        // 组件列表（只考虑组件）
        // components: componentsReducer,
        //#endregion

        //#region 加了undo的写法
        components: undoable(componentsReducer, {
            limit: 20, // 限制 undo 20步
            // 屏蔽一些不需要undo的action
            filter: excludeAction([
                "components/resetComponents",
                "components/changeSelectedId",
                "components/selectPrevComponent",
                "components/selectNextComponent"
            ])
        }),
        //#endregion

        // 页面信息  （标题title  描述 desc ... ）
        pageInfo: pageInfoReducer
    }
})

export default store
